package com.isoft.demo01.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isoft.demo01.dao.GameDao;
import com.isoft.demo01.dao.ShopDao;
import com.isoft.demo01.entity.Game;
import com.isoft.demo01.entity.Order;
import com.isoft.demo01.entity.Shop;
import com.isoft.demo01.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

@Service
public class ShopImplService extends ServiceImpl<GameDao, Game> implements ShopService {
    @Autowired
    ShopDao shopDao;

    @Autowired
    GameDao gameDao;


    @Override
    public int getCartQuantity(Integer userid) {
        return shopDao.findCartQuantity(userid);
    }

    @Override
    public boolean addcart(Integer userid, Integer gameid) {
        Shop cart = shopDao.findCart(userid, gameid);

        if (cart == null){//未购买，添加购物项
            cart = new Shop();
            cart.setUserid(userid);
            cart.setGameid(gameid);
            return shopDao.addcart(userid, gameid)> 0;
        } else {  //已购买，修改购物项，增加数量
            return false;
        }
    }


    public  List<Shop> getCartVOList(Integer userid) {
        return  shopDao.findCartList(userid);
    }

    @Override
    public boolean deleteCart(Integer id) {
        return shopDao.deleteByid(id) > 0;
    }

    @Override
    public boolean deleteCartBatch(Integer[] ids,Integer userid) {
        return shopDao.deleteCartBatch(Arrays.asList(ids),userid)>0;
    }

    @Override
    public boolean clearCart(Integer userid) {
        Order order=new Order();
        int r1=(int)(Math.random()*(10));//产生2个0-9的随机数
        int r2=(int)(Math.random()*(10));
        long now = System.currentTimeMillis();//一个13位的时间戳
        order.setOrderid(String.valueOf(r1)+String.valueOf(r2)+String.valueOf(now));// 订单ID
        return shopDao.clearCart(userid)>0;
    }

    @Override
    public int allprice(Integer userid) {
        return shopDao.allprice(userid);
    }


}
